package com.ruoyi.community.service.impl.integral;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.integral.IntDetails;
import com.ruoyi.community.domain.integral.IntUserStatistic;
import com.ruoyi.community.mapper.integral.IntDetailsMapper;
import com.ruoyi.community.service.integral.IIntDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 积分详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-08
 */
@Service
public class IntDetailsServiceImpl implements IIntDetailsService 
{
    @Autowired
    private IntDetailsMapper intDetailsMapper;
    @Autowired
    private IntUserStatisticServiceImpl userStatisticService;

    /**
     * 查询积分详情
     * 
     * @param id 积分详情ID
     * @return 积分详情
     */
    @Override
    public IntDetails selectIntDetailsById(Long id)
    {
        return intDetailsMapper.selectIntDetailsById(id);
    }

    /**
     * 查询积分详情列表
     * 
     * @param intDetails 积分详情
     * @return 积分详情
     */
    @Override
    public List<IntDetails> selectIntDetailsList(IntDetails intDetails)
    {
        intDetails.setCommunityId(SecurityUtils.getCommunityId());
        return intDetailsMapper.selectIntDetailsList(intDetails);
    }

    /**
     * 新增积分详情
     * 
     * @param detail 需要传入 operationId content(操作内容,例如：可口可乐广告，2020年物业费) type(0:支出，1：收入) integral（积分值）
     * @return 结果
     */
    @Override
    @Transactional
    public int insertIntDetails(IntDetails detail)
    {
        detail.setUserId(SecurityUtils.getUserID());
        detail.setCommunityId(SecurityUtils.getCommunityId());
        detail.setDate(new Date());
        // 新增成功后要修改 int_user_statistic（用户积分信息统计） 表
        IntUserStatistic userStatistic = userStatisticService.selectIntUserStatisticById(detail.getUserId(), detail.getCommunityId());
        if (detail.getType() == 1) {
            userStatistic.setTotal(userStatistic.getTotal() + detail.getIntegral());
        } else {
            userStatistic.setConsume(userStatistic.getConsume() - detail.getIntegral());
        }

        intDetailsMapper.insertIntDetails(detail);
        return userStatisticService.updateIntUserStatistic(userStatistic);
    }

    /**
     * 修改积分详情
     * 
     * @param intDetails 积分详情
     * @return 结果
     */
    @Override
    public int updateIntDetails(IntDetails intDetails)
    {
        return intDetailsMapper.updateIntDetails(intDetails);
    }

    /**
     * 批量删除积分详情
     * 
     * @param ids 需要删除的积分详情ID
     * @return 结果
     */
    @Override
    public int deleteIntDetailsByIds(Long[] ids)
    {
        return intDetailsMapper.deleteIntDetailsByIds(ids);
    }

    /**
     * 删除积分详情信息
     * 
     * @param id 积分详情ID
     * @return 结果
     */
    @Override
    public int deleteIntDetailsById(Long id)
    {
        return intDetailsMapper.deleteIntDetailsById(id);
    }

    @Override
    public Integer isFirst(IntDetails details) {
        return intDetailsMapper.isFirst(details);
    }
}
